<?php

namespace app\app\controller;

use app\app\model\ProjectCheckModel;
use app\app\model\ProjectDKModel;
use app\app\model\ProjectLZModel;
use app\app\model\ProjectMeetingModel;
use app\app\model\ProjectModel;
use app\app\model\UserModel;
use app\BaseController;
use app\Request;


class ProjectPerform extends BaseController
{
    /**
     * 颗粒度数据统计数据
     * @param Request $request
     */
    public function analysis(Request $request)
    {
        $data = $request->param();
        $user_name = input('user_name', '');
        $start = input('start_time', '');
        $end = input('end_time', '');
        $list_time = $this->time($data['type'], $start, $end);
        $start_time = $list_time['start_time'];
        $end_time = $list_time['end_time'];

        //打卡查询条件
        $where_tj = " dk_time_in BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";

        //发现问题查询条件
        $where_problem = " check_time BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";

        //会议查询条件
        $where_meet = " meeting_time BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";

        if ($user_name) {
            $where_tj .= " AND user_name  LIKE '%" . $user_name . "%'";
            $where_problem .= " AND check_user  LIKE '%" . $user_name . "%'";
            $where_meet .= " AND user_name  LIKE '%" . $user_name . "%'";
        }

        //发现问题总次数
        $problem_total = ProjectCheckModel::where('check_source','业主检查')->where($where_problem)->count('id');

        //打卡总次数
        $total = ProjectDKModel::where($where_tj)->count('id');

        //会议次数

        $meet_total = ProjectMeetingModel::where($where_meet)->count('id');

        //已履职人数
//        $lz_times = ProjectLZModel::where($where_tj)->group('user_name,project_id')->count('id');

        //总履职次数
//        $lz_total_arr = $this->project_user($start_time, $end_time);

        $tong_ji = array(
            'total' => $total,
            'problem_total' => $problem_total,
            'meet_total' => $meet_total
        );

//        $where = "1=1";
//        if ($data['type'] == 1) {
//            $where .= " AND a.lz_time BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";
//        } elseif ($data['type'] == 2) {
//            $where .= " AND a.lz_time BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";
//        } else {
//            if (isset($data['start_time']) && isset($data['end_time'])) {
//                $where .= " AND a.lz_time BETWEEN  '" . $data['start_time'] . "'  AND '" . $data['end_time'] . "'";
//            } else {
//                if (isset($data['start_time'])) $where .= " AND a.lz_time  > '" . $data['start_time'] . "'";
//                if (isset($data['end_time'])) $where .= " AND a.lz_time  < '" . $data['end_time'] . "'";
//            }
//        }


//        $list = ProjectLZModel::alias('a')->field('a.id,a.user_name,b.project_name,COUNT(a.id) as total')
//            ->join('t_project b', 'a.project_id=b.id')
//            ->where($where)->group('a.user_name')->order('total desc')->select();


        $list_dk = ProjectDKModel::field('user_name,count(id) as total')->where($where_tj)->group('user_name')->select()->toArray();
        $list_meet = ProjectMeetingModel::field('user_name,count(id) as total')->where($where_meet)->group('user_name')->select()->toArray();
        $list_check = ProjectCheckModel::field('check_user as user_name,count(id) as total')->where('check_source','业主检查')->where($where_problem)->group('user_name')->select()->toArray();

        $user_list = array_unique(array_column(array_merge($list_dk, array_merge($list_meet, $list_check)), 'user_name'));

        $info = [];
        foreach ($user_list as $key => $value) {
            $info[$key]['user_name'] = $value;
            $info[$key]['dk_total'] = 0;
            $info[$key]['meet_total'] = 0;
            $info[$key]['check_total'] = 0;
            foreach ($list_dk as $k => $v) {
                if ($value == $v['user_name']) {
                    $info[$key]['dk_total'] += $v['total'];
                }
            }
            foreach ($list_check as $k => $v) {
                if ($value == $v['user_name']) {
                    $info[$key]['check_total'] = $v['total'];
                }
            }
            foreach ($list_meet as $k => $v) {
                if ($value == $v['user_name']) {
                    $info[$key]['meet_total'] += $v['total'];
                }
            }
        }
        $res = array(
            'tong_ji' => $tong_ji,
            'list' => $info

        );
        $this->success('success', $res);
    }


    public function project_user($start_time, $end_time)
    {
        $where[] = ['tc_time', '>=', $start_time];
        $where[] = ['kg_time', '<=', $end_time];
        $where[] = ['is_parent', '=', 1];
        $where[] = ['project_period', '=', 2];
        $list = ProjectModel::field('project_name,link_user_id')->where($where)->select();
        $user_info = UserModel::field('user_name,nick_name')->where('del_flag', 0)->select();

        $project_user = array();
        foreach ($list as $key => $value) {
            $user_arr = explode(',', $value['link_user_id']);
            foreach ($user_arr as $k => $v) {
                if ($v) {
                    foreach ($user_info as $m => $n) {
                        if ($v == $n['user_name']) {
                            $item['user_name'] = $n['nick_name'];
                        }
                    }
                    $item['project_name'] = $value['project_name'];
                    array_push($project_user, $item);
                }

            }
        }
        return $project_user;
    }


    public function perform()
    {
        $type = input('type', '1');
        $time_type = input('time_type', '1');
        $date = date("Y-m-d");
        if ($time_type == 1) {
            $timestamp = time();
            $start_time = date('Y-m-d 00:00:00', strtotime("this week Monday", $timestamp));
            $end_time = date("Y-m-d 23:59:59", strtotime("$start_time +6 day"));
        } else {
            $start_time = date('Y-m-01 00:00:00', strtotime($date));
            $end_time = date('Y-m-d 23:59:59', strtotime("$start_time +1 month -1 day"));
        }
        $where_tj = " lz_time BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";
        if ($type == 1) { //已履职人员
            $list = ProjectLZModel::alias('a')->field("a.user_name,DATE_FORMAT(a.lz_time,'%Y-%m-%d') as time,b.project_name")
                ->leftJoin('t_project b', 'a.project_id = b.id')
                ->where($where_tj)->group('time,a.user_name,b.project_name')->select();

        } else {
            $list_info = ProjectLZModel::alias('a')->field("a.user_name,DATE_FORMAT(a.lz_time,'%Y-%m-%d') as time,b.project_name")
                ->leftJoin('t_project b', 'a.project_id = b.id')
                ->where($where_tj)->group('time,a.user_name,b.project_name')->select()->toArray();
            $list = $this->project_user($start_time, $end_time);
            foreach ($list as $key => $value) {
                foreach ($list_info as $k => $v) {
                    if ($v['user_name'] == $value['user_name'] && $v['project_name'] == $value['project_name']) {
                        unset($list[$key]);
                    }
                }
            }
        }
        $this->success('success', $list);
    }


    public function list()
    {
        $project_name = input('project_name');
        $page_size = input('pageSize', '20');

        $where = "1=1";
        if (isset($project_name)) $where .= " AND b.project_name  LIKE '%" . $project_name . "%'";

        $list = ProjectLZModel::alias('a')->field('a.user_name,a.lz_time,a.lz_duration,b.project_name')
            ->leftjoin('t_project b', 'b.id = a.project_id')
            ->where($where)->paginate($page_size);
        $this->success('success', $list);

    }

    public function detail()
    {
        $data = $this->request->param();
        $type = input('type', '1');

        $where = "a.user_name  LIKE '%" . $data['user_name'] . "%'";
        $date = date("Y-m-d");
        $timestamp = time();
        if ($type == 1) {
            $start_time = date('Y-m-d 00:00:00', strtotime("this week Monday", $timestamp));
            $end_time = date("Y-m-d 23:59:59", strtotime("$start_time +6 day"));
            $where .= " AND a.lz_time BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";
        } elseif ($data['type'] == 2) {
            $start_time = date('Y-m-01 00:00:00', strtotime($date));
            $end_time = date('Y-m-d 23:59:59', strtotime("$start_time +1 month -1 day"));
            $where .= " AND a.lz_time BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";
        } else {
            if (isset($data['start_time']) && isset($data['end_time'])) {
                $where .= " AND a.lz_time BETWEEN  '" . $data['start_time'] . "'  AND '" . $data['end_time'] . "'";
            } else {
                if (isset($data['start_time'])) $where .= " AND a.lz_time  > '" . $data['start_time'] . "'";
                if (isset($data['end_time'])) $where .= " AND a.lz_time  < '" . $data['end_time'] . "'";
            }
        }

        $page_size = input('pageSize', '20');
        $list = ProjectLZModel::alias('a')->field('a.user_name,a.lz_time,a.lz_duration,b.project_name')
            ->leftjoin('t_project b', 'b.id = a.project_id')
            ->where($where)->paginate($page_size);
        $this->success('success', $list);
    }


    /**
     * 某个时间内所有用户或指定用户打卡次数
     */
    public function signIn()
    {
        $user_name = input('user_name');
        $time_type = input('time_type', '1');

        $start = input('start_time', '');
        $end = input('end_time', '');
        $list_time = $this->time($time_type, $start, $end);
        $start_time = $list_time['start_time'];
        $end_time = $list_time['end_time'];

        $where = " dk_time_in BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";
        if (isset($user_name)) $where .= " AND user_name  LIKE '%" . $user_name . "%'";

        $list = ProjectDKModel::field('user_name,COUNT(id) as total')
            ->where($where)->group('user_name')->select();
        $this->success('success', $list);
    }


    /**
     * 用户打卡列表信息数据
     */
    public function signInDetail()
    {
        $user_name = input('user_name');
        $time_type = input('time_type', '1');
        $page_size = input('pageSize', '20');

        $start = input('start_time', '');
        $end = input('end_time', '');
        $list_time = $this->time($time_type, $start, $end);
        $start_time = $list_time['start_time'];
        $end_time = $list_time['end_time'];

        $where = "  a.user_name  LIKE '%" . $user_name . "%'";
        $where .= " AND a.dk_time_in BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";

        $list = ProjectDKModel::alias('a')
            ->field('a.*,b.project_name')
            ->leftJoin('t_project b', 'a.project_id = b.id')
            ->order('a.dk_time_in desc')
            ->where($where)->paginate($page_size);

        $this->success('success', $list);
    }


    /**
     * 某个时间内所有用户或指定用户问题次数
     */
    public function problem()
    {
        $user_name = input('user_name');
        $time_type = input('time_type', '1');

        $start = input('start_time', '');
        $end = input('end_time', '');
        $list_time = $this->time($time_type, $start, $end);
        $start_time = $list_time['start_time'];
        $end_time = $list_time['end_time'];

        $where = " check_time BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";
        if (isset($user_name)) $where .= " AND check_user  LIKE '%" . $user_name . "%'";

        $list = ProjectCheckModel::field('check_user,COUNT(id) as total')
            ->where($where)->group('check_user')->select();
        $this->success('success', $list);
    }

    /**
     * 问题列表信息数据
     */
    public function problemDetail()
    {
        $user_name = input('user_name');
        $user_name = input('user_name');
        $time_type = input('time_type', '1');
        $page_size = input('pageSize', '20');
        if ($user_name) {
            $start = input('start_time', '');
            $end = input('end_time', '');
            $list_time = $this->time($time_type, $start, $end);
            $start_time = $list_time['start_time'];
            $end_time = $list_time['end_time'];

            $where = "  a.check_user  LIKE '%" . $user_name . "%'";
            $where .= " AND a.check_time BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";
        } else {
            $project_id = input('project_id');
            $where = "  a.project_id  = '" . $project_id . "'";
        }

        $list = ProjectCheckModel::alias('a')
            ->field('a.*,b.peccancy_type,b.peccancy_content')
            ->leftjoin('t_project_peccancy b', 'a.peccancy_id = b.id')
            ->order('a.check_time desc')
            ->where($where)->paginate($page_size);

        $this->success('success', $list);
    }


    /**
     * 某个时间内所有用户或指定用户会议次数
     */
    public function meeting()
    {
        $user_name = input('user_name');
        $time_type = input('time_type', '1');


        $start = input('start_time', '');
        $end = input('end_time', '');
        $list_time = $this->time($time_type, $start, $end);
        $start_time = $list_time['start_time'];
        $end_time = $list_time['end_time'];

        $where = " meeting_time BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";
        if (isset($user_name)) $where .= " AND user_name  LIKE '%" . $user_name . "%'";

        $list = ProjectMeetingModel::field('user_name,COUNT(id) as total')
            ->where($where)->group('user_name')->select();
        $this->success('success', $list);
    }

    /**
     * 用户会议列表信息数据
     */
    public function meetingDetail()
    {
        $user_name = input('user_name');
        $time_type = input('time_type', '1');

        $start = input('start_time', '');
        $end = input('end_time', '');
        $list_time = $this->time($time_type, $start, $end);
        $start_time = $list_time['start_time'];
        $end_time = $list_time['end_time'];

        $where = "  user_name  LIKE '%" . $user_name . "%'";
        $where .= " AND a.meeting_time BETWEEN  '" . $start_time . "'  AND '" . $end_time . "'";


        $list = ProjectMeetingModel::alias('a')->field('a.*,b.project_name,c.file_name,c.file_path')
            ->leftJoin('t_project b', 'b.id = a.project_id')
            ->leftJoin('sys_file_info c', 'a.meeting_file_id = c.file_id')
            ->where($where)->order('a.meeting_time desc ')->select();
        $this->success('success', $list);
    }


    /**
     * 时间区间
     * @param $type
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function time($type, $start_time, $end_time)
    {
        if ($type == 1) {
            $start_time = date('Y-m-d 00:00:00', time());
            $end_time = date('Y-m-d 23:59:59', time());
        } elseif ($type == 2) {  //当周
            $start_time = date('Y-m-d 00:00:00', strtotime("this week Monday", time()));
            $end_time = date("Y-m-d 23:59:59", strtotime("$start_time +6 day"));
        } elseif ($type == 3) {  //当月
            $start_time = date('Y-m-01 00:00:00', time());
            $end_time = date('Y-m-d 23:59:59', strtotime("$start_time +1 month -1 day"));
        } else {

            if (!empty($start_time) && !empty($end_time)) {
                $start_time = $start_time;
                $end_time = $end_time;
            } else {
                $start_time = date('Y-01-01 00:00:00', time());
                $end_time = date('Y-12-31 23:59:59', time());
            }
        }
        $list = array(
            'start_time' => $start_time,
            'end_time' => $end_time,
        );
        return $list;
    }

}
